home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / prog / cfuncs.zip / STRING.C < prev    next >
C/C++ Source or Header  |  1992-01-15  |  6KB  |  236 lines

  1. #include <ctype.h>
  2. /*----------------------------------------------------------------*/
  3. /*-----------------------------blanks-----------------------------*/
  4. /*DESCRIPTION: tests whether a string consists entirely of blanks */
  5. /*                                  */
  6. /*INPUT: a null terminated string                  */
  7. /*RETURNS: 1 if the string is blank                  */
  8. /*         0 if the string is not blank                  */
  9. /*USES: nothing                              */
  10. /*IN: string.c                              */
  11. /*----------------------------------------------------------------*/
  12.  
  13. blanks(char *string)
  14.  
  15. {
  16.     while(isspace(*string) && *string != '\0')
  17.       ++string;
  18.  
  19.     return(*string == '\0');
  20. }
  21.  
  22. /*-----------------------------------------------------------*/
  23. /*----------------------strspc-------------------------------*/
  24. /*DESCRIPTION: Puts len spaces into string and adds a         */
  25. /*    terminating null char.                     */
  26. /*                                 */
  27. /*RETURNS: the address of string                 */
  28. /*USES: nothing                             */
  29. /*IN: string.c                             */
  30. /*-----------------------------------------------------------*/
  31.  
  32. char *strspc(char *string, int len)
  33. {
  34.     string[len] = '\0';
  35.     while(len > 0)
  36.        string[--len] = ' ';
  37.  
  38.     return(string);
  39. }
  40.  
  41. /*-----------------------------------------------------*/
  42. /*---------------strnchr-------------------------------*/
  43. /*DESCRIPTION: puts len copies of ch into string and   */
  44. /*    adds a terminating null ch.               */
  45. /*                               */
  46. /*RETURNS: the address of string               */
  47. /*USES: nothing.                       */
  48. /*IN: string.c                           */
  49. /*-----------------------------------------------------*/
  50.  
  51. char *strnchr(char *string, int ch, int len)
  52. {
  53.     string[len] = '\0';
  54.     while(len>0)
  55.         string[--len] = ch;
  56.  
  57.         return(string);
  58. }
  59.  
  60. /*------------------------------------------------------*/
  61. /*---------------------rtrim----------------------------*/
  62. /*DESCRIPTION: Puts a null char directly after the last */
  63. /*    non-blank char in a null terminated string.    */
  64. /*                            */
  65. /*RETURNS: the address of string            */
  66. /*USES: nothing                        */
  67. /*IN: string.c                        */
  68. /*------------------------------------------------------*/
  69.  
  70. char *rtrim(char *string)
  71. {
  72.     int     nonblank = -1, i=0;
  73.  
  74.     for(; string[i] != '\0'; ++i)
  75.       if(string[i] != ' ')
  76.          nonblank = i;
  77.  
  78.     string[nonblank+1] = '\0';
  79.  
  80.     return(string);
  81. }
  82.  
  83. /*------------------------------------------------------*/
  84. /*---------------------rtrimlen-------------------------*/
  85. /*RETURNS: the length of s not counting trailing blanks */
  86. /*USES: nothing                        */
  87. /*IN: string.c                        */
  88. /*------------------------------------------------------*/
  89.  
  90. int rtrimlen(const char *s)
  91. {
  92.     int  nonblank=-1, i=0;
  93.  
  94.     for(; s[i] != '\0'; ++i)
  95.        if(s[i] != ' ')
  96.         nonblank = i;
  97.  
  98.     return(nonblank+1);
  99. }
  100.  
  101. /*------------------------------------------------------------ */
  102. /*-----------------------MidStr------------------------------- */
  103. /*DESCRIPTION: Copies n chars from src to dest followed by the */
  104. /*    null ch.                           */
  105. /*                                   */
  106. /*RETURNS: the address of dest                     */
  107. /*USES: nothing                               */
  108. /*IN: string.c                               */
  109. /*------------------------------------------------------------ */
  110.  
  111. char *MidStr (char *dest, const char *src, int n)
  112. {
  113.    int i;
  114.  
  115.    for(i=0; i < n; ++i)
  116.      dest[i] = src[i];
  117.  
  118.    dest[n] = '\0';
  119.    return(dest);
  120. }
  121.  
  122. /*------------------------------------------------------*/
  123. /*---------------ltrim----------------------------------*/
  124. /*DESCRIPTION: Takes the leading spaces out of a string.*/
  125. /*                            */
  126. /*RETURNS: the address of string            */
  127. /*USES: MidStr                        */
  128. /*IN: string.c                         */
  129. /*------------------------------------------------------*/
  130.  
  131. char *ltrim(char *string)
  132. {
  133.     int     FirstNonBlank=0, i = -1;
  134.  
  135.     do
  136.     {
  137.        ++i;
  138.        if(!FirstNonBlank && string[i] != ' ')
  139.           if((FirstNonBlank = i)==0)
  140.             break;
  141.        if(FirstNonBlank)
  142.           string[i-FirstNonBlank] = string[i];
  143.     }while(string[i] != '\0');
  144.  
  145.     return(string);
  146. }
  147.  
  148. /*------------------------------------------------------*/
  149. /*---------------charpos--------------------------------*/
  150. /*DESCRIPTION: Finds the position of ch in src.         */
  151. /*                                                      */
  152. /*RETURNS: The the address of string                  */
  153. /*USES: Nothing                                         */
  154. /*IN: string.c                                         */
  155. /*------------------------------------------------------*/
  156. int charpos( const char *src, char ch)
  157. {
  158.      int p = 0;
  159.      if (strlen(src) == 0)
  160.      return(1 == 0);
  161.  
  162.      while( src[p] != ch )
  163.      if (src[p++] == '\x0')
  164.           return (1 == 0);
  165.  
  166.      return(p);
  167. }
  168.  
  169. /*------------------------------------------------------*/
  170. /*---------------Add_Comma------------------------------*/
  171. /*DESCRIPTION: Converts a real number into a formatted  */
  172. /*             string with commas and decimals          */
  173. /*                                                      */
  174. /*EXAMPLE: 1000.23 becomes $1,000.23                    */
  175. /*RETURNS: The the address of string            */
  176. /*USES: Nothing                                         */
  177. /*IN: string.c                         */
  178. /*------------------------------------------------------*/
  179. char *add_comma(double x, int dec, int dolsign)
  180. {
  181.      char buffer[40] = "";
  182.      char dblstr[40] = "";
  183.      int i, l, cnt, index;
  184.  
  185.      sprintf(dblstr, "%30.*f", dec, x);
  186.      ltrim(dblstr);
  187.  
  188.      l = strlen(dblstr);
  189.      strspc(buffer, 39);
  190.  
  191.      if (dec)
  192.          {
  193.          for (i=l-1 ,cnt=0; cnt<dec+1; --i, ++cnt)
  194.               buffer[cnt] = dblstr[i];
  195.  
  196.          for (i=l-dec-2,cnt=dec+1,index=0; i>=0; --i, ++cnt)
  197.               {
  198.               buffer[cnt] = dblstr[i];
  199.               index++;
  200.               if (index == 3 && i>0)
  201.                    {
  202.                buffer[++cnt] = ',';
  203.                index=0;
  204.                    }
  205.               }
  206.          }/* End If Dollar Sign */
  207.      else
  208.          for (i=l-1,cnt=0, index=0; i>=0; --i, ++cnt)
  209.               {
  210.               buffer[cnt] = dblstr[i];
  211.               index++;
  212.               if (index == 3 && i>0)
  213.                    {
  214.                buffer[++cnt] = ',';
  215.                index=0;
  216.                    }
  217.               }
  218.      buffer[cnt] = '\0';
  219.  
  220.      if (dolsign)
  221.          strcat(buffer, "$");
  222.  
  223.      strrev(buffer);
  224.  
  225.      return(buffer);
  226. }
  227.  
  228. /*
  229. void main(void)
  230. {
  231.     double batchbal=5000;
  232.  
  233.     printf("%s\n", add_comma(batchbal, 10, 0));
  234.  
  235. }
  236.  */